<template>
  <!-- 插槽内容 -->
  <el-row type="flex" align="midden" :class="{border:isRoot}" justify="space-between" style="height:40px; line-height:40px; width: 100%">
    <!-- 左侧内容 -->
    <el-col>
      <span><i class="el-icon-s-custom" /> {{ treeNode.name }}</span>
    </el-col>
    <!-- 右侧内容 -->
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col>{{ treeNode.manager }}</el-col>
        <!-- 下拉菜单 element -->
        <el-col>
          <el-dropdown @command="operateDepts">
            <span>操作 <i class="el-icon-arrow-down" /></span>
            <el-dropdown-menu slot="dropdown">
              <!-- 下拉选项 -->
              <el-dropdown-item command="add" disable="CheckPermission('add-deit')">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
        <!-- /下拉菜单 element -->
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartement } from '@/api/departments'
export default {
  props: {
    // 定义传入属性
    treeNode: {
      required: true,
      type: Object
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    // 点击 编辑 删除 和新增时触发
    operateDepts(type) {
      if (type === 'add') {
        // 添加子部门
        this.$emit('addDepts', this.treeNode)
      } else if (type === 'edit') {
        // 编辑部门
        this.$emit('editDepts', this.treeNode)
      } else {
        // 删除部门
        this.$confirm('您确定删除此部门吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          await delDepartement(this.treeNode.id)
          this.$emit('delDepts')
          // console.log(this.treeNode)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      }
    }
  }

}
</script>

<style scoped>
.border {
  border-bottom: 1px solid black;
}
</style>
